Micron Document
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| SparkN0de-git | SparkN0de |
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Node / ReticulumProjects / Sideband.git / files / sbapp / kivymd / theming_dynamic_text.py

Displaying Raw • Download


sbapp/kivymd/theming_dynamic_text.py 0.2.2 (399da923) Text, 2.92 KB

T8b949e"""
Theming Dynamic Text
====================

Two implementations. The first is based on color brightness obtained from-
https://www.w3.org/TR/AERT#color-contrast
The second is based on relative luminance calculation for sRGB obtained from-
https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef
and contrast ratio calculation obtained from-
https://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef

Preliminary testing suggests color brightness more closely matches the
`Material Design spec` suggested text colors, but the alternative implementation
is both newer and the current 'correct' recommendation, so is included here
as an option.
"""


Tff7b72def Td2a8ff_color_brightnessTb4b4b4(Te6edf3colorTb4b4b4)Tb4b4b4:
T8b949e# Implementation of color brightness method
Te6edf3brightness Tff7b72= Te6edf3colorTb4b4b4[T79c0ff0Tb4b4b4] Tff7b72* T79c0ff299 Tff7b72+ Te6edf3colorTb4b4b4[T79c0ff1Tb4b4b4] Tff7b72* T79c0ff587 Tff7b72+ Te6edf3colorTb4b4b4[T79c0ff2Tb4b4b4] Tff7b72* T79c0ff114
Te6edf3brightness Tff7b72= Te6edf3brightness
Tff7b72return Te6edf3brightness


Tff7b72def Td2a8ff_black_or_white_by_color_brightnessTb4b4b4(Te6edf3colorTb4b4b4)Tb4b4b4:
Tff7b72if Te6edf3_color_brightnessTb4b4b4(Te6edf3colorTb4b4b4) Tff7b72>Tff7b72= T79c0ff500Tb4b4b4:
Tff7b72return Ta5d6ff"Ta5d6ffblackTa5d6ff"
Tff7b72elseTb4b4b4:
Tff7b72return Ta5d6ff"Ta5d6ffwhiteTa5d6ff"


Tff7b72def Td2a8ff_normalized_channelTb4b4b4(Te6edf3colorTb4b4b4)Tb4b4b4:
T8b949e# Implementation of contrast ratio and relative luminance method
Tff7b72if Te6edf3color Tff7b72<Tff7b72= T79c0ff0.03928Tb4b4b4:
Tff7b72return Te6edf3color Tff7b72/ T79c0ff12.92
Tff7b72elseTb4b4b4:
Tff7b72return Tb4b4b4(Tb4b4b4(Te6edf3color Tff7b72+ T79c0ff0.055Tb4b4b4) Tff7b72/ T79c0ff1.055Tb4b4b4) Tff7b72*Tff7b72* T79c0ff2.4


Tff7b72def Td2a8ff_luminanceTb4b4b4(Te6edf3colorTb4b4b4)Tb4b4b4:
Te6edf3rg Tff7b72= Te6edf3_normalized_channelTb4b4b4(Te6edf3colorTb4b4b4[T79c0ff0Tb4b4b4]Tb4b4b4)
Te6edf3gg Tff7b72= Te6edf3_normalized_channelTb4b4b4(Te6edf3colorTb4b4b4[T79c0ff1Tb4b4b4]Tb4b4b4)
Te6edf3bg Tff7b72= Te6edf3_normalized_channelTb4b4b4(Te6edf3colorTb4b4b4[T79c0ff2Tb4b4b4]Tb4b4b4)
Tff7b72return T79c0ff0.2126 Tff7b72* Te6edf3rg Tff7b72+ T79c0ff0.7152 Tff7b72* Te6edf3gg Tff7b72+ T79c0ff0.0722 Tff7b72* Te6edf3bg


Tff7b72def Td2a8ff_black_or_white_by_contrast_ratioTb4b4b4(Te6edf3colorTb4b4b4)Tb4b4b4:
Te6edf3l_color Tff7b72= Te6edf3_luminanceTb4b4b4(Te6edf3colorTb4b4b4)
Te6edf3l_black Tff7b72= T79c0ff0.0
Te6edf3l_white Tff7b72= T79c0ff1.0
Te6edf3b_contrast Tff7b72= Tb4b4b4(Te6edf3l_color Tff7b72+ T79c0ff0.05Tb4b4b4) Tff7b72/ Tb4b4b4(Te6edf3l_black Tff7b72+ T79c0ff0.05Tb4b4b4)
Te6edf3w_contrast Tff7b72= Tb4b4b4(Te6edf3l_white Tff7b72+ T79c0ff0.05Tb4b4b4) Tff7b72/ Tb4b4b4(Te6edf3l_color Tff7b72+ T79c0ff0.05Tb4b4b4)
Tff7b72return Ta5d6ff"Ta5d6ffwhiteTa5d6ff" Tff7b72if Te6edf3w_contrast Tff7b72>Tff7b72= Te6edf3b_contrast Tff7b72else Ta5d6ff"Ta5d6ffblackTa5d6ff"


Tff7b72def Td2a8ffget_contrast_text_colorTb4b4b4(Te6edf3colorTb4b4b4, Te6edf3use_color_brightnessTff7b72=Tff7b72TrueTb4b4b4)Tb4b4b4:
Tff7b72if Te6edf3use_color_brightnessTb4b4b4:
Te6edf3contrast_color Tff7b72= Te6edf3_black_or_white_by_color_brightnessTb4b4b4(Te6edf3colorTb4b4b4)
Tff7b72elseTb4b4b4:
Te6edf3contrast_color Tff7b72= Te6edf3_black_or_white_by_contrast_ratioTb4b4b4(Te6edf3colorTb4b4b4)
Tff7b72if Te6edf3contrast_color Tff7b72== Ta5d6ff"Ta5d6ffwhiteTa5d6ff"Tb4b4b4:
Tff7b72return T79c0ff1Tb4b4b4, T79c0ff1Tb4b4b4, T79c0ff1Tb4b4b4, T79c0ff1
Tff7b72elseTb4b4b4:
Tff7b72return T79c0ff0Tb4b4b4, T79c0ff0Tb4b4b4, T79c0ff0Tb4b4b4, T79c0ff1


Tff7b72if Tff7b72__name__ Tff7b72== Ta5d6ff"Ta5d6ff__main__Ta5d6ff"Tb4b4b4:
Tff7b72from T7ee787kivyT7ee787.T7ee787utils Tff7b72import Te6edf3get_color_from_hex

Tff7b72from T7ee787kivymdT7ee787.T7ee787color_definitions Tff7b72import Te6edf3colorsTb4b4b4, Te6edf3text_colors

Tff7b72for Te6edf3c Tff7b72in Te6edf3colorsTff7b72.Td2a8ffitemsTb4b4b4(Tb4b4b4)Tb4b4b4:
Tff7b72if Te6edf3cTb4b4b4[T79c0ff0Tb4b4b4] Tff7b72in Tb4b4b4[Ta5d6ff"Ta5d6ffLightTa5d6ff"Tb4b4b4, Ta5d6ff"Ta5d6ffDarkTa5d6ff"Tb4b4b4]Tb4b4b4:
Tff7b72continue
Te6edf3color Tff7b72= Te6edf3cTb4b4b4[T79c0ff0Tb4b4b4]
Tffa657printTb4b4b4(Ta5d6fffTa5d6ff"Ta5d6ffFor the Tffd700{Te6edf3colorTffd700}Ta5d6ff color palette:Ta5d6ff"Tb4b4b4)
Tff7b72for Te6edf3nameTb4b4b4, Te6edf3hex_color Tff7b72in Te6edf3cTb4b4b4[T79c0ff1Tb4b4b4]Tff7b72.Td2a8ffitemsTb4b4b4(Tb4b4b4)Tb4b4b4:
Tff7b72if Te6edf3hex_colorTb4b4b4:
Te6edf3col Tff7b72= Te6edf3get_color_from_hexTb4b4b4(Te6edf3hex_colorTb4b4b4)
Te6edf3col_bri Tff7b72= Te6edf3get_contrast_text_colorTb4b4b4(Te6edf3colTb4b4b4)
Te6edf3con_rat Tff7b72= Te6edf3get_contrast_text_colorTb4b4b4(
Te6edf3colTb4b4b4, Te6edf3use_color_brightnessTff7b72=Tff7b72False
Tb4b4b4)
Te6edf3text_color Tff7b72= Te6edf3text_colorsTb4b4b4[Te6edf3cTb4b4b4[T79c0ff0Tb4b4b4]Tb4b4b4]Tb4b4b4[Te6edf3nameTb4b4b4]
Tffa657printTb4b4b4(
Ta5d6fffTa5d6ff"Ta5d6ff The Tffd700{Te6edf3nameTffd700}Ta5d6ff hue gives Tffd700{Te6edf3col_briTffd700}Ta5d6ff using color Ta5d6ff"
Ta5d6fffTa5d6ff"Ta5d6ffbrightness, Tffd700{Te6edf3con_ratTffd700}Ta5d6ff using contrast ratio, and Ta5d6ff"
Ta5d6fffTa5d6ff"Tffd700{Te6edf3text_colorTffd700}Ta5d6ff from the MD specTa5d6ff"
Tb4b4b4)


──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────